﻿using Furion.CMS.Core.Base;
using Furion.CMS.Domain;
using Furion.DataValidation;
using Furion.LinqBuilder;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Furion.CMS.Application
{
    public class LoginRecordFilter : BaseFilterParams<UserLoginRecord>
    {
        public DateTimeOffset? StartTime { get; set; }
        public DateTimeOffset? EndTime { get; set; }
        public string Host { get; set; }
        public string Account { get; set; }

        /// <summary>
        /// 获取过滤表达式
        /// </summary>
        /// <returns></returns>
        public override Expression<Func<UserLoginRecord, bool>> GetFilterExpression()
        {
            Expression<Func<UserLoginRecord, bool>> condition = x => true;
            if (StartTime.HasValue)
            {
                condition = condition.And(x => x.LoginTime >= StartTime.Value.Date);
            }
            if (EndTime.HasValue)
            {
                EndTime = EndTime.Value.AddDays(1);
                condition = condition.And(x => x.LoginTime < EndTime.Value.Date);
            }
            if (!string.IsNullOrWhiteSpace(Host))
            {
                condition = condition.And(x => x.Host.Contains(Host));
            }
            if (!string.IsNullOrWhiteSpace(Account))
            {
                condition = condition.And(x => x.Account == Account);
            }
            return condition;
        }
    }
}
